package com.lxm.pwhelp.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;

import com.lxm.pwhelp.R;
import com.lxm.pwhelp.bean.Item;
import com.lxm.pwhelp.dao.PWItemDao;
import com.lxm.pwhelp.utils.Tools;
/**
 * edit the item activity
 * @author Listener
 * @version 2015-6-28 14:51:01
 */
public class EditItemActivity extends Activity implements View.OnClickListener {

	private PWItemDao itemDao;
	
	private EditText name;
	private EditText username;
	private EditText password;
	private EditText url;
	private EditText comment;
	private EditText question1;
	private EditText question2;
	private Spinner type;
	private RadioGroup subtype;
	private String defaultStr,bankStr,webStr,weiboStr,qqStr,emailStr,alipayStr;
	
	private LinearLayout item1,item2,item3,item4,item5,item6,item7,item8,item9;
	
	private Resources resources;

	private ArrayAdapter<String> adapter;
	private String typeStr;
	private Bundle bundle;
	private TextView title;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.edit_layout);
		title=(TextView)findViewById(R.id.title);
		title.setText(this.getResources().getText(R.string.edit_title));
		findViewById(R.id.Return).setOnClickListener(this);
		findViewById(R.id.Edit_Submit).setOnClickListener(this);
		bundle = this.getIntent().getExtras();
		init(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.Return:
			Intent intent = new Intent(EditItemActivity.this, MainActivity.class);
			setResult(RESULT_OK, intent);
			finish();
			break;
		case R.id.Edit_Submit:
			String nameStr = name.getText().toString();
			String usernameStr = username.getText().toString();
			String passwordStr = password.getText().toString();
			String urlStr = url.getText().toString();
			String commentStr = comment.getText().toString();
			String question1Str = question1.getText().toString();
			String question2Str = question2.getText().toString();

			if (emptyStr(usernameStr))
				new AlertDialog.Builder(this).setTitle("警告")
						.setMessage("用户名不能为空！").setPositiveButton("确定", null)
						.show();
			else if (emptyStr(passwordStr))
				new AlertDialog.Builder(this).setTitle("警告")
						.setMessage("密码不能为空！").setPositiveButton("确定", null)
						.show();
			else {
				Item item = (Item) bundle.getSerializable("item");
				item.setItem_name(nameStr);
				item.setItem_username(usernameStr);
				item.setItem_password(passwordStr);
				item.setItem_type(typeStr == null ? "" : typeStr);
				switch(subtype.getCheckedRadioButtonId()){
				case R.id.radioCash:
					item.setItem_subtype(0);
					break;
				case R.id.radioCredit:
					item.setItem_subtype(1);
					break;
				}
				item.setItem_url(urlStr);
				item.setItem_comment(commentStr);
				item.setQuestion1(question1Str);
				item.setQuestion2(question2Str);
				item.setModified(Tools.getToday());
				itemDao.updateItem(item);
				new AlertDialog.Builder(this)
						.setTitle("修改成功")
						.setMessage("密码项修改成功,返回！")
						.setPositiveButton(
								"确定",
								new android.content.DialogInterface.OnClickListener() {
									@Override
									public void onClick(
											DialogInterface dialog,
											int which) {
										Intent intent = new Intent(EditItemActivity.this, MainActivity.class);
										setResult(RESULT_OK, intent);
										finish();
									}
						}).show();
			}
			break;
		}
	}

	private boolean emptyStr(String str) {
		if (str != null && str.trim().length() > 0)
			return false;
		else
			return true;
	}

	private void init(Context context) {
		Item item = (Item) bundle.getSerializable("item");
		resources = this.getResources();
		itemDao = new PWItemDao(this);
		name = (EditText) findViewById(R.id.edit_name);
		username = (EditText) findViewById(R.id.edit_username);
		password = (EditText) findViewById(R.id.edit_password);
		url = (EditText) findViewById(R.id.edit_url);
		comment = (EditText) findViewById(R.id.edit_comment);
		question1 = (EditText) findViewById(R.id.edit_question1);
		question2 = (EditText) findViewById(R.id.edit_question2);
		type = (Spinner) findViewById(R.id.edit_type);
		subtype = (RadioGroup) findViewById(R.id.radioGroup);
		
		name.setText(item.getItem_name());
		username.setText(item.getItem_username());
		password.setText(item.getItem_password());
		url.setText(item.getItem_url());
		comment.setText(item.getItem_comment());
		question1.setText(item.getQuestion1());
		question2.setText(item.getQuestion2());
		subtype.setSelected(false);
		
		item1 = (LinearLayout) findViewById(R.id.item1);
		item2 = (LinearLayout) findViewById(R.id.item2);
		item3 = (LinearLayout) findViewById(R.id.item3);
		item4 = (LinearLayout) findViewById(R.id.item4);
		item5 = (LinearLayout) findViewById(R.id.item5);
		item6 = (LinearLayout) findViewById(R.id.item6);
		item7 = (LinearLayout) findViewById(R.id.item7);
		item8 = (LinearLayout) findViewById(R.id.item8);
		item9 = (LinearLayout) findViewById(R.id.item9);
		
		// 类型分组名称
		defaultStr = resources.getString(R.string.group_default);
		bankStr = resources.getString(R.string.group_bank);
		webStr = resources.getString(R.string.group_web);
		weiboStr = resources.getString(R.string.group_weibo);
		qqStr = resources.getString(R.string.group_qq);
		emailStr = resources.getString(R.string.group_email);
		alipayStr = resources.getString(R.string.group_alipay);
		
		// 将可选内容与ArrayAdapter连接起来
		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_dropdown_item, new String[]{defaultStr,bankStr,webStr,weiboStr,qqStr,emailStr,alipayStr});
		// 设置下拉列表的风格
		//adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// 将adapter 添加到spinner中
		type.setAdapter(adapter);
		//type.setTag(item.getItem_type());
		int position = getPostion(item.getItem_type());
		type.setSelection(position,true);
		// 添加事件Spinner事件监听
		type.setOnItemSelectedListener(new SpinnerSelectedListener());
	}

	// 使用数组形式操作
	class SpinnerSelectedListener implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> adapter, View view, int position,
				long id) {
			typeStr = adapter.getSelectedItem().toString();
			if(defaultStr.equals(typeStr)){
				item1.setVisibility(View.VISIBLE);
				item3.setVisibility(View.VISIBLE);
				item4.setVisibility(View.VISIBLE);
				item6.setVisibility(View.VISIBLE);
				item2.setVisibility(View.GONE);
				item5.setVisibility(View.GONE);
				item7.setVisibility(View.GONE);
				item8.setVisibility(View.GONE);
				item9.setVisibility(View.GONE);
			}else if(bankStr.equals(typeStr)){
				item1.setVisibility(View.VISIBLE);
				item3.setVisibility(View.VISIBLE);
				item4.setVisibility(View.VISIBLE);
				item6.setVisibility(View.VISIBLE);
				item2.setVisibility(View.GONE);
				item5.setVisibility(View.GONE);
				item7.setVisibility(View.GONE);
				item8.setVisibility(View.GONE);
				item9.setVisibility(View.VISIBLE);
			}else if(webStr.equals(typeStr)){
				item1.setVisibility(View.VISIBLE);
				item3.setVisibility(View.VISIBLE);
				item4.setVisibility(View.VISIBLE);
				item5.setVisibility(View.VISIBLE);
				item6.setVisibility(View.VISIBLE);
				item2.setVisibility(View.GONE);
				item7.setVisibility(View.GONE);
				item8.setVisibility(View.GONE);
				item9.setVisibility(View.GONE);
			}else if(weiboStr.equals(typeStr)){
				item1.setVisibility(View.VISIBLE);
				item3.setVisibility(View.VISIBLE);
				item4.setVisibility(View.VISIBLE);
				item6.setVisibility(View.VISIBLE);
				item5.setVisibility(View.VISIBLE);
				item2.setVisibility(View.GONE);
				item7.setVisibility(View.GONE);
				item8.setVisibility(View.GONE);
				item9.setVisibility(View.GONE);
			}else if(qqStr.equals(typeStr)){
				item1.setVisibility(View.VISIBLE);
				item2.setVisibility(View.VISIBLE);
				item3.setVisibility(View.VISIBLE);
				item4.setVisibility(View.VISIBLE);
				item6.setVisibility(View.VISIBLE);
				item7.setVisibility(View.VISIBLE);
				item8.setVisibility(View.VISIBLE);
				item5.setVisibility(View.GONE);
				item9.setVisibility(View.GONE);
			}else if(emailStr.equals(typeStr)){
				item1.setVisibility(View.VISIBLE);
				item3.setVisibility(View.VISIBLE);
				item4.setVisibility(View.VISIBLE);
				item6.setVisibility(View.VISIBLE);
				item5.setVisibility(View.VISIBLE);
				item2.setVisibility(View.VISIBLE);
				item7.setVisibility(View.GONE);
				item8.setVisibility(View.GONE);
				item9.setVisibility(View.GONE);
			}else if(alipayStr.equals(typeStr)){
				item1.setVisibility(View.VISIBLE);
				item3.setVisibility(View.VISIBLE);
				item4.setVisibility(View.VISIBLE);
				item6.setVisibility(View.VISIBLE);
				item2.setVisibility(View.GONE);
				item5.setVisibility(View.GONE);
				item7.setVisibility(View.GONE);
				item8.setVisibility(View.GONE);
				item9.setVisibility(View.GONE);
			}else{
				item1.setVisibility(View.VISIBLE);
				item3.setVisibility(View.VISIBLE);
				item4.setVisibility(View.VISIBLE);
				item6.setVisibility(View.VISIBLE);
				item2.setVisibility(View.GONE);
				item5.setVisibility(View.GONE);
				item7.setVisibility(View.GONE);
				item8.setVisibility(View.GONE);
				item9.setVisibility(View.GONE);
			}
			
		}

		public void onNothingSelected(AdapterView<?> arg0) {
		}
	}
	
	private int getPostion(String type){
		if(defaultStr.equals(type)){
			return 0;
		}else if(bankStr.equals(type)){
			return 1;
		}else if(webStr.equals(type)){
			return 2;
		}else if(weiboStr.equals(type)){
			return 3;
		}else if(qqStr.equals(type)){
			return 4;
		}else if(emailStr.equals(type)){
			return 5;
		}else if(alipayStr.equals(type)){
			return 6;
		}
		return -1;
	}
}